<?php

namespace Admin\Controller;

use Org\Util;

/**
 * Description of LbsProjectAreaMouldController
 *
 * @author Myron
 */
class LbsProjectAreaMouldController extends AdminController {

    public function index($id = '') {
        $name = I('name');
        $map['pid'] = $id;
        $map['name'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('lbs_project_areamould', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $projectdata = M('lbs_project')->where(array('id' => $id))->find();
        $this->assign('_data', $projectdata);
        $this->meta_title = '标题';
        $this->display();
    }

    /**
     * 添加
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function add($id = '', $pid = '', $name = '', $remark = '') {
        if (IS_POST) {
            $gr_lbs_project_areamould = array(
                'pid' => $pid,
                'name' => $name,
                'remark' => $remark,
                'delflag' => 0,
                'createat' => NOW_TIME
            );
            $ids = M('lbs_project_areamould')->add($gr_lbs_project_areamould);
            if (!$ids) {
                $this->error('添加失败！');
            } else {
                $this->success('添加成功！', U('index', array('id' => $pid)));
            };
        } else {
            $projectdata = M('lbs_project')->where(array('id' => $id))->find();
            $this->assign('_data', $projectdata);
            $this->display();
        }
    }

    /**
     * 状态修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function changeStatus($method = null, $id) {
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'forbid':
                $this->forbid('lbs_project_areamould', $map);
                break;
            case 'resume':
                $this->resume('lbs_project_areamould', $map);
                break;
            case 'delete':

                M('lbs_project_area')->where(array('mid' => $id))->setField('status', -1);
                $this->delete('lbs_project_areamould', $map);
                break;
            default:
                $this->error('lbs_project_areamould');
        }
    }

    /*     * *
     * 设定某个图层项目为激活状态
     */

    public function activate($pid = '', $mid = '') {
        $gr_lbs_project_areamould = array(
            'activate' => 0,
        );
        M('lbs_project_areamould')->where(array('pid' => $pid))->save($gr_lbs_project_areamould);
        
        if (!M('lbs_project_areamould')->where(array('id' => $mid))->setField('activate', 1)) {
            $this->error('修改失败！');
        } else {
            $map['pid'] = $pid;
            $map['status'] = array('eq', 1);
            M('lbs_project_area')->where($map)->setField('status', 0);
            $map['mid'] = $mid;
            $map['status'] = array('eq', 0);
            M('lbs_project_area')->where($map)->setField('status', 1);
            
            
            $arealist = M('lbs_project_area')->where($map)->select();
            for ($i = 0; $i < count($arealist); $i++) {
                $areainfoid=$arealist[$i]['areainfoid'];
                $warehouseid= M('lbs_project_areainfo')->where(array('id'=>$areainfoid))->getField('warehouseid');
                M('lbs_project_area')->where(array('areainfoid'=>$areainfoid))->setField('warehouse', $warehouseid);
            } 
            
            $this->success('修改成功！', U('index', array('id' => $pid)));
        }
    }

    /**
     * 复制
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function copy($id = '', $pid = '', $name = '', $remark = '', $mid = '',$aid='') {
        if (IS_POST) {
            $gr_lbs_project_areamould = array(
                'pid' => $pid,
                'name' => $name,
                'remark' => $remark,
                'delflag' => 0,
                'createat' => NOW_TIME
            );
            $ids = M('lbs_project_areamould')->add($gr_lbs_project_areamould);
            if (!$ids) {
                $this->error('添加失败！');
            } else {
                $newmid = $ids;
                $map['mid'] = $mid;
                $map['status'] =array('neq',-1);
                $arealist = M('lbs_project_area')->where($map)->select();
                for ($i = 0; $i < count($arealist); $i++) {
                    $gr_lbs_project_area = array(            
                        'areaname' => $arealist[$i]['areaname'],
                        'name' =>$arealist[$i]['name'],
                        'phone' =>$arealist[$i]['phone'],
                        'warehouse' =>$arealist[$i]['warehouse'],
                        'points' =>$arealist[$i]['points'],
                        'areainfoid' => $arealist[$i]['areainfoid'],
                        'status' => 0,
                        'pid' => $pid,
                        'aid' => $aid,
                        'mid' => $newmid,
                        'createat' => NOW_TIME, 
                        'delflag' => 0,
                    );
                    $id = M('lbs_project_area')->add($gr_lbs_project_area);
                }
                $this->success('添加成功！', U('index', array('id' => $pid)));
            };
        } else {
            $moulddata = M('lbs_project_areamould')->where(array('id' => $mid))->find();
            $projectdata = M('lbs_project')->where(array('id' => $pid))->find();
            $this->assign('_data', $projectdata);
            $this->assign('_moulddata', $moulddata);
            $this->display();
        }
    }

}
